package org.example.code.test2025.three_month;

import java.util.*;

public class test0322 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        long[] a = new long[n];
        for(int i=0;i<n;i++){
            a[i]=in.nextLong();
        }
        System.out.println(fun2(n,m,a));
    }

    private static long fun2(int n, int m, long[] a) {
        Set<Long> sets = new HashSet<>();
        sets.add(0L);
        for(long num:a){
            Set<Long> temp = new HashSet<>(sets);
            for(long t:sets){
                temp.add((t+num)%m);
            }
            sets=temp;
        }
        for(long i=m-1;i>=0;i--){
            if(sets.contains(i)){
                return i;
            }
        }
        return 0;
    }

    private static int fun(int n, int d, List<Integer> list) {
        int temp = list.get(0)%d;
        boolean f = true;
        for(int num:list){
            if(num%d!=temp){
                f=false;
                break;
            }
        }
        if(!f){
            return -1;
        }
        int max = Integer.MIN_VALUE;
        for(int num:list){
            int q=num/d;
            max=Math.max(q,max);
        }
        int result = 0;
        for(int num:list){
            result=result+max-(num/d);
        }
        return result;
    }
}
